Lab9


4531117521_4531205021  นาย เอื้อ บุณยะประภัศร์ และ น.ส. จุฑาภรณ์ ลิ่มปิติกุล (10/9/2545 (12:01:31))
(SM=3, CM=25, ST=32, KY=1, TR=00:00)

Applet
Source Code
public class DecimalLED extends SevenSegmentLED {

  private int n;
  public void setDigit(int d) {
//    SevenSegmentLED led = new SevenSegmentLED();
 
  boolean[][] s =
  
  {{true, true, true, true, true, true, false},
  {false, true, true, false, false, false, false},
  {true, true, false, true, true, false, true},
  {true, true, true, true, false, false, true},
  {false, true, true, false, false, true, true},
  {true, false, true, true, false, true, true},
  {true, false, true, true, true, true, true},
  {true, true, true, false, false, false, false},
  {true, true, true, true, true, true, true},
  {true, true, true, true, false, true, true}};
   
  if ((d <= 9) && (d >= 0))
  this.setSegments(s[d]);
  


 
 
  
  
  
  
  
  
  
  
  super.setSegments(s[d]);



  }

  public int getDigit() {

    return n;
  }

}

import jlab.JLabIO; import java.awt.*; import java.awt.event.*; import java.applet.*; public class Lab9 extends Applet implements Runnable { private DecimalLED[] display = new DecimalLED[3]; public void init() { for (int i = display.length - 1; i >= 0; i--) { display[i] = new DecimalLED(); add(display[i]); display[i].setSize(40, 80); } validate(); } public void start() { (new Thread(this)).start(); } public void run() { int k; display[0].setDigit(0); while (true) { for (int i = 0; i < display.length; i++) { k = Math.max(display[i].getDigit(), 0); display[i].setDigit((++k) % 10); if (k < 10) break; } try { Thread.sleep(100); } catch (InterruptedException e) {} } } public static void main(String[] args) { Frame frame = new Frame("Lab9 : 7 segments"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); Lab9 applet = new Lab9(); frame.add(applet, BorderLayout.CENTER); frame.setSize(200, 120); frame.setVisible(true); applet.init(); applet.start(); } }
import java.awt.*; public class SevenSegmentLED extends Canvas { private boolean[] segments; private static final int[] NUMPOINTS = {5, 5, 7, 5, 5, 5, 5}; private static final int[][][] SEGMENTS = { {{5, 35, 30, 10, 5, 0, 0}, {5, 5, 10, 10, 5, 0, 0}}, {{35, 35, 30, 30, 35, 0, 0}, {8, 37, 34, 13, 8, 0, 0}}, {{35, 35, 30, 30, 35, 0, 0}, {43, 72, 67, 46, 43, 0, 0}}, {{5, 35, 30, 10, 5, 0, 0}, {75, 75, 70, 70, 75, 0, 0}}, {{5, 5, 10, 10, 5, 0, 0}, {43, 72, 67, 46, 43, 0, 0}}, {{5, 5, 10, 10, 5, 0, 0}, {8, 37, 34, 13, 8, 0, 0}}, {{10, 30, 35, 30, 10, 5, 10}, {37, 37, 40, 43, 43, 40, 37}} }; public SevenSegmentLED() { setBackground(new Color(0, 0, 0)); setForeground(new Color(0, 255, 0)); } public void setSegments(boolean[] s) { if (s != null) { segments = new boolean[s.length]; for (int i = 0; i < s.length; i++) { segments[i] = s[i]; } repaint(); } } public void paint(Graphics g) { double scalex = (double) bounds().width / 40.0; double scaley = (double) bounds().height / 80.0; Color f = getForeground(); g.setColor(f); for (int i = 0; i < 7; i++) { if (segments != null && i < segments.length && segments[i]) { int[][] points = new int[2][NUMPOINTS[i]]; for (int j = 0; j < NUMPOINTS[i]; j++) { points[0][j] = (int) (SEGMENTS[i][0][j] * scalex); points[1][j] = (int) (SEGMENTS[i][1][j] * scaley); } g.fillPolygon(points[0], points[1], NUMPOINTS[i]); } } } }